﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class VacationEdit : System.Web.UI.Page
    {
        protected ReqVacationMain reqVacationMain;
        protected string empName;
        private ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string formNo = Request.QueryString["formNo"];
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReqVacationMain>(r => r.ReqVacationSubs);
                ctx.LoadOptions = dlo;
                reqVacationMain = ctx.ReqVacationMains.Single(r => r.FormNo == formNo);

                FormNo.Value = reqVacationMain.FormNo;
                VacationType.Value = reqVacationMain.VacationType;
                DaysCnt.Value = reqVacationMain.DaysCnt.ToString();
                ReplacedDay.Value = reqVacationMain.ReplacedDay;
                //Cause.Value = reqVacationMain.Cause;

                rptList.DataSource = reqVacationMain.ReqVacationSubs;
                rptList.DataBind();
                Employee employee = ctx.Employees.Single(r => r.Id == reqVacationMain.EmpId);
                empName = employee.Name;

                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                string sql = string.Format(@"select top 3 b.Id,b.FormNo,b.PreStartTime,b.PreEndTime,a.ReplacedDay Remarks  from ReqVacationMains a,ReqVacationSubs b where a.FormNo=b.FormNo and a.EmpId={0} and ReqDate>=DATEADD(M,-6,GETDATE()) and a.VacationType='调休' and (a.StatusId=2 or a.StatusId=5) order by PreStartTime desc;", ebi.Id);
                IEnumerable<ReqVacationSub> reqVacationSubs = ctx.ExecuteQuery<ReqVacationSub>(sql);
                rptList0.DataSource = reqVacationSubs;
                rptList0.DataBind();

                sql = string.Format(@"select top 3 * from ReqOvertime
  where EmpId={0} and ReqDate>=DATEADD(M,-6,GETDATE())
  order by FormNo desc;", ebi.Id);
                IEnumerable<ReqOvertime> reqOvertimes = ctx.ExecuteQuery<ReqOvertime>(sql);
                rptList2.DataSource = reqOvertimes;
                rptList2.DataBind();

                sql = string.Format(@"select top 3 * from duty
  where EmpId={0} and dutydate>=DATEADD(M,-6,GETDATE())
  order by id desc;", ebi.Id);
                IEnumerable<Duty> duty = ctx.ExecuteQuery<Duty>(sql);
                rptList3.DataSource = duty;
                rptList3.DataBind();

                Common common = new Common();
                int nianJiaTotal = common.GetNianJia(ebi.Id.ToString());
                lblNianJiaTotal.InnerText = nianJiaTotal.ToString();

                sql = string.Format(@"SELECT ISNULL(SUM(DaysCnt),0)
  FROM ReqVacationMains a,ReqVacationSubs b
  where a.FormNo=b.FormNo and a.EmpId={0} and VacationType='年假' and b.PreStartTime>='{1}' and StatusId=2;", ebi.Id.ToString(), DateTime.Today.ToString("yyyy-01-01"));
                lblNianJia.InnerText = ctx.ExecuteQuery<decimal>(sql).First().ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _formSave(0);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _formSave(1);
        }
        private void _formSave(int isSubmit)
        {
            reqVacationMain = ctx.ReqVacationMains.Single(r => r.FormNo == FormNo.Value);
            reqVacationMain.ReqDate = DateTime.Now;
            reqVacationMain.VacationType = VacationType.Value;
            reqVacationMain.DaysCnt = decimal.Parse(DaysCnt.Value);
            reqVacationMain.ReplacedDay = ReplacedDay.Value;
            //reqVacationMain.Cause = Cause.Value;
            reqVacationMain.StatusId = isSubmit;

            //更新时删除之前的签核数据
            var list = from r in ctx.SignFlows where r.FormNo == FormNo.Value select r;
            ctx.SignFlows.DeleteAllOnSubmit(list);

            ctx.SubmitChanges();
            if (isSubmit == 1)//写签核流程
            {
                FlowSet fs = new FlowSet();
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                fs.SetSignFlow(FormNo.Value, ebi);
            }
            Response.Write("<script>alert('修改成功');window.close();</script>");
        }
    }
}